package com.shengyin.ec.service.account;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shengyin.ec.model.dao.account.PersonalDao;
import com.shengyin.ec.model.entity.GoodsType;
import com.shengyin.ec.model.entity.Personal;

@Service("personalService")
public class PersonalServiceImpl  implements PersonalService{
	@Autowired
	PersonalDao personalDao;

	@Override
	public void createPersonal(Personal personal) {
		personalDao.save(personal);		
	}

	@Override
	public void updatePersonal(Personal personal) {
		
		try {
			System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+personal.getId());
			Personal personal1=personalDao.get(personal.getId());
			BeanUtils.copyProperties(personal1, personal);
			personalDao.update(personal1);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public Personal getPersonalById(long id) {
		return personalDao.get(id);
	}

	@Override
	public void removePersonalById(long id) {
		personalDao.deleteByKey(id);		
	}

	@Override
	public List<Personal> getPersonalList() {
		List<Personal> list=	personalDao.loadAll();
		return list;
	}

	@Override
	public List<Personal> getPersonalByPage(int offset, int pageSize) {
		@SuppressWarnings("unchecked")
		List<Personal> list =personalDao.findByPage("from Personal p", offset, pageSize);
		return list;
	}

	@Override
	public void deleteAllPersonal(String[] ids) {
		for(String id:ids)
		{
			if(id!=null)
				personalDao.deleteByKey(Long.valueOf(id));
		}
		
	}
	
	//add by lcl 2012-3-8
		@Override
		public List<Personal> getPersonalByUsername(String username){
			List<Personal> personal = personalDao.getPersonalByUsername(username);
			return personal;
		}
		
		public List<Personal> getPersonalByUsername(String username,int offset,int pageSize){
			
			return personalDao.getPersonalByUsername(username,offset,pageSize);
		}
		@Override
		public boolean checkUser(String userName, String pwd){
			boolean isCorrect = personalDao.checkUser(userName, pwd);
			return isCorrect;
		}

	//add by wql 2012-3-20
		@Override
		public Long getTotal(){
			return personalDao.getTotal();
		}
		
		@Override
		public List getPersonalList(int offset,int pageSize){
			return personalDao.getPersonalList(offset,pageSize);
		}
}
